package cn.tedu.web.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.tedu.web.entity.User;
import cn.tedu.web.entity.Wallet;
import cn.tedu.web.entity.WalletAccount;
import cn.tedu.web.mapper.WalletMapper;
import cn.tedu.web.service.IWalletAccountService;
import cn.tedu.web.service.IWalletService;
import cn.tedu.web.utils.ResponseUtil;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 【我的钱包】 前端控制器
 * </p>
 * wallet
 * 
 * @author issen
 * @since 2021-10-23
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/api/wallet")
public class WalletController {

    @Autowired
    public IWalletService walletService;
    @Autowired
    public IWalletAccountService walletAccountService;
    @Autowired
    public WalletMapper walletMapper;

    /**
     * 查询账户余额
     *
     * @param uid 用户ID
     */
    @ApiOperation(value = "查询指定账户的交易流水")
    @GetMapping("/getAmount/{uid}")
    public BigDecimal getAmount(@PathVariable("uid") Integer uid) {
        return walletMapper.findAmountById(uid);
    }

    /**
     * 查询交易流水
     *
     * @param uid 用户ID
     */
    @ApiOperation(value = "查询指定账户的交易流水")
    @GetMapping("/list/{uid}")
    public List<WalletAccount> list(@PathVariable("uid") Integer uid, HttpSession session) {
        // System.out.print(walletService.findAll(uid));
        QueryWrapper queryWrapper = new QueryWrapper<Wallet>();
        queryWrapper.eq("uid", uid);
        List<WalletAccount> walletAccounts = walletAccountService.list(queryWrapper);
        // return walletService.findAll(uid);
        return walletAccounts;
    }

    /**
     * 初始化支付密码
     *
     * @param password 支付密码
     */
    @ApiOperation(value = "初始化支付密码")
    @PostMapping("init")
    public Integer init(@RequestBody Wallet wallet, HttpSession session) {
        String password = wallet.getPassword();
        Integer uid = wallet.getUid();
        return walletService.initPaymentPassword(uid, password);
    }

    @ApiOperation(value = "充值")
    @PostMapping("/recharge")
    public ResponseUtil<Object> recharge(@RequestBody Wallet wallet, HttpSession session) {
        String password = wallet.getPassword();
        Double amount = wallet.getAmount().doubleValue();
        Integer uid = wallet.getUid(); // 获取当前用户的ID
        Integer status = walletService.recharge(uid, password, amount);
        QueryWrapper queryWrapper = new QueryWrapper<WalletAccount>();
        queryWrapper.eq("uid", uid);
        User user = new User();
        user.setWalletAccounts(walletAccountService.list(queryWrapper));
        user.setAmount(walletMapper.selectOne(queryWrapper.select("amount")).getAmount());
        String msg = "";
        // -1,-2, >0
        if (status == -1) {
            msg = "用户不存在";
            return new ResponseUtil<>(-1, msg, user);
        } else if (status == -2) {
            msg = "密码错误";
            return new ResponseUtil<>(-2, msg, user);
        } else {
            msg = "充值完成";
            return new ResponseUtil<>(1, msg, user);
        }
    }

    /**
     * 支付
     * 
     * @param password 支付密码
     * @param orderid  订单编号
     */
    @ResponseBody
    @PostMapping("pay")
    public String pay(@RequestBody JSONObject param, HttpSession session) {
        String password = param.getString("password");
        String orderid = param.getString("orderid");
        Integer uid = param.getInteger("uid");
        // Integer uid = (Integer) session.getAttribute(Constant.SESSION_KEY);
        Integer status = walletService.pay(uid, password, orderid);
        String response = "";
        if (status == -1) {
            response = "参数异常";
        } else if (status == -2) {
            response = "支付密码错误";
        } else if (status == -3) {
            response = "订单不存在";
        } else if (status == -4) {
            response = "订单已支付";
        } else if (status > 0) {
            response = "支付完成";
        } else {
            response = "未经处理的异常！";
        }
        return response;
    }

}
